﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYElectricSetting
{
	public partial class FittingSelectForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

		public FittingSelectForm(string title, Document document, BuiltInCategory filter, List<DbFamilyObject> objects)
		{
			this.InitializeComponent();
			this.formTitle = title;
			this.doc = document;
			this.filterCategory = filter;
			this.familyObjects = objects;
			this.familySymbol = null;
		}

		private void FittingSelectForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.Text = this.formTitle;
			this.InitListView();
			this.InitListBox();
		}

		private void InitListBox()
		{
			this.lsbName.Items.Clear();
			foreach (DbFamilyObject dbFamilyObject in this.familyObjects)
			{
				this.lsbName.Items.Add(dbFamilyObject.Name);
			}
			bool flag = this.lsbName.Items.Count > 0;
			if (flag)
			{
				this.lsbName.SelectedIndex = 0;
			}
		}

		private void InitListView()
		{
			this.lsvImage.LargeImageList.ImageSize = new Size(90, 90);
			this.lsvImage.Clear();
			this.lsvImage.BeginUpdate();
			foreach (DbFamilyObject dbFamilyObject in this.familyObjects)
			{
				bool flag = string.IsNullOrEmpty(dbFamilyObject.Image) || !File.Exists(dbFamilyObject.Image);
				if (!flag)
				{
					Image image = Image.FromFile(dbFamilyObject.Image);
					this.lsvImage.LargeImageList.Images.Add(dbFamilyObject.Name, image);
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Name = dbFamilyObject.Name;
					listViewItem.Text = dbFamilyObject.Name;
					listViewItem.Tag = dbFamilyObject;
					listViewItem.ImageKey = dbFamilyObject.Name;
					this.lsvImage.Items.Add(listViewItem);
				}
			}
			this.lsvImage.EndUpdate();
			bool flag2 = this.lsvImage.Items.Count <= 0;
			if (flag2)
			{
				base.DialogResult = DialogResult.Cancel;
				base.Close();
			}
			else
			{
				FittingSelectForm.SendMessage(this.lsvImage.Handle, this.LVM_SETICONSPACING, 0, 7864418);
			}
		}

		private void lsbName_SelectedIndexChanged(object sender, EventArgs e)
		{
			string key = this.lsbName.SelectedItem.ToString();
			ListViewItem listViewItem = this.lsvImage.Items[key];
			bool flag = listViewItem == null;
			if (!flag)
			{
				listViewItem.Selected = true;
				listViewItem.EnsureVisible();
			}
		}

		private void lsvImage_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool flag = this.lsvImage.SelectedItems.Count <= 0;
			if (!flag)
			{
				this.lsbName.SelectedItem = this.lsvImage.SelectedItems[0].Text;
			}
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			bool flag = this.lsvImage.SelectedItems.Count <= 0;
			if (flag)
			{
				TaskDialog.Show("盈建科提示", "请选择任意一项，再加载。");
			}
			else
			{
				DbFamilyObject dbFamilyObject = this.lsvImage.SelectedItems[0].Tag as DbFamilyObject;
				bool flag2 = this.doc == null || dbFamilyObject == null || !File.Exists(dbFamilyObject.Family);
				if (flag2)
				{
					TaskDialog.Show("盈建科提示", "当前选项对应的族文件不存在。");
				}
				else
				{
					this.familySymbol = this.LoadFamily(dbFamilyObject);
					base.DialogResult = ((this.familySymbol == null) ? DialogResult.Cancel : DialogResult.OK);
					base.Close();
				}
			}
		}

		private FamilySymbol LoadFamily(DbFamilyObject familyObject)
		{
			FamilySymbol result = null;
			Family family = null;
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(familyObject.Family);
			Transaction transaction = null;
			try
			{
				List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(this.doc, typeof(FamilySymbol), this.filterCategory);
				foreach (Element element in elementByCategory)
				{
					FamilySymbol familySymbol = element as FamilySymbol;
					bool flag = familySymbol.Family.Name == fileNameWithoutExtension;
					if (flag)
					{
						return familySymbol;
					}
				}
				transaction = new Transaction(this.doc, "LoadFamily");
				transaction.Start();
				bool flag2 = this.doc.LoadFamily(familyObject.Family, out family);
				transaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
				bool flag3 = transaction != null && transaction.GetStatus() != (TransactionStatus)3;
				if (flag3)
				{
					transaction.RollBack();
				}
			}
			bool flag4 = family != null;
			if (flag4)
			{
				List<FamilySymbol> symbols = family.GetSymbols();
				using (List<FamilySymbol>.Enumerator enumerator2 = symbols.GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						FamilySymbol familySymbol2 = enumerator2.Current;
						result = familySymbol2;
					}
				}
			}
			return result;
		}

		public FamilySymbol GetFamilySymbol()
		{
			return this.familySymbol;
		}

		private int LVM_SETICONSPACING = 4149;

		private string formTitle = string.Empty;

		private Document doc = null;

		private BuiltInCategory filterCategory;

		private List<DbFamilyObject> familyObjects = new List<DbFamilyObject>();

		private FamilySymbol familySymbol = null;
	}
}
